// http.js
// 通常可以吧 baseUrl 单独放在一个 js 文件了
//const baseUrl = 'http://192.168.1.6'
const baseUrl = 'http://8.130.163.141'
const apiUrl = baseUrl+'/api';

const request = (options = {}) => {
	// 在这里可以对请求头进行一些设置
	// 例如：
	options.header = {
		"Content-Type": "application/x-www-form-urlencoded"
	}
	
	uni.showLoading({
		title: "加载中",
		mask: true,
	});
	
	return new Promise((resolve, reject) => {
		uni.request({
			url: apiUrl + options.url || '',
			method: options.type || 'GET',
			data: options.data || {},
			header: options.header || {}
		}).then(data => {
			let [err, res] = data;
			resolve(res);
			uni.hideLoading();
		}).catch(error => {
			reject(error)
		})
	});
}

const get = (url, data, options = {}) => {
	options.type = 'GET';
	options.data = data;
	options.url = url;
	return request(options)
}

const post = (url, data, options = {}) => {
	options.type = 'POST';
	options.data = data;
	options.url = url;
//	 console.log(JSON.stringify(options))
	return request(options)
}

export default {
	request,
	get,
	post,
	baseUrl,
	apiUrl
}
